//
//  AssignStaffRoomTimeCell.m
//  QRMaster
//
//  Created by myApple on 16/5/20.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "AssignStaffRoomTimeCell.h"
#import "setTimeModel.h"

typedef NS_ENUM(NSInteger,StaffRoomTimeType) {
    StaffRoomTimeTypeFirst = 1120,
    StaffRoomTimeTypeSeconde,
    StaffRoomTimeTypeThird,
};

@interface AssignStaffRoomTimeCell()

@property(nonatomic,strong)UIImageView * roomImageView;
@property(nonatomic,copy)UILabel * roomNameLabel;
@property(nonatomic,copy)UILabel * floorNameLabel;
@property(nonatomic,copy)UILabel * buildingNameLabel;

/**
 *  拥有的时间周期
 */
@property(nonatomic,retain)NSArray * haveTimeArray;
@property(nonatomic,strong)AssignRoomTimeModel * roomInfoModel;

@end

@implementation AssignStaffRoomTimeCell

#pragma mark ---------init
+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * identityId = @"assignRoomTimeCell";
    AssignStaffRoomTimeCell * cell = [tableView dequeueReusableCellWithIdentifier:identityId];
    if (!cell) {
        cell = [[AssignStaffRoomTimeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identityId];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self addAllSubView];
        [self prepareTimeView];
    }
    return self;
}

-(void)addAllSubView
{
    [self addSubview:self.roomImageView];
    [self addSubview:self.roomNameLabel];
    [self addSubview:self.floorNameLabel];
    [self addSubview:self.buildingNameLabel];
}

#pragma mark ------------ 准备时间图标视图
-(void)prepareTimeView
{
    float width = 16;
    float x = 0;
    float y = 8;
    float height = 16;
    for (int i = 0; i < 3; i++) {
        x += width + 10;
        width = 16;
        UIButton * timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        timeBtn.hidden = YES;
        timeBtn.tag = StaffRoomTimeTypeFirst + i;
        timeBtn.frame = CGRectMake(x, y, width, height);
        [self.contentView addSubview:timeBtn];
        [timeBtn setImage:[UIImage imageNamed:[NSString stringWithFormat: @"roomTime%d",(i + 1)]] forState:0];
    }
}


#pragma mark ----------initData
-(void)setData:(AssignRoomTimeModel *)roomInfo
{
    self.roomInfoModel = roomInfo;
    self.roomNameLabel.text = self.roomInfoModel.roomName;
    self.floorNameLabel.text = self.roomInfoModel.floorName;
    self.buildingNameLabel.text = self.roomInfoModel.bulidingName;
    self.haveTimeArray = self.roomInfoModel.timeArray;
}

-(void)setAllTimeArray:(NSMutableArray *)allTimeArray
{
    _allTimeArray = allTimeArray;
}

#pragma mark-------view
-(void)layoutSubviews
{
    [super layoutSubviews];
    float x = 16;
    float y = (self.frame.size.height - 39)/2.0;
    float width = 39;
    float height = 39;
    self.roomImageView.frame = CGRectMake(x, y, width, height);
    
    x += width + 10;
    y = 6;
    width = [self calculateWidth:(self.frame.size.width - x - 32 - 80) string:self.roomInfoModel.roomName Font:16.0];
    height = 20;
    self.roomNameLabel.frame = CGRectMake(x, y, width, height);
    
    //时间图片
    for (int i = 0; i < 3; i ++) {
        UIButton * btn = (UIButton *)[self viewWithTag:StaffRoomTimeTypeFirst +i];
        btn.hidden = YES;//防止复用图片
        btn.backgroundColor = [UIColor whiteColor];
    }
    NSInteger index = 0;
    for (setTimeModel * timeModel in _allTimeArray) {
        UIButton * btn = (UIButton *)[self viewWithTag:StaffRoomTimeTypeFirst + index];
        for (int i = 0 ; i < self.haveTimeArray.count; i ++)
        {
            setTimeModel * model = self.haveTimeArray[i];
            if ([model.timeId isEqualToString:timeModel.timeId])
            {
                btn.hidden = NO;
                btn.frame = CGRectMake(width + x  + 10 + 26 * i, btn.frame.origin.y, 16, btn.frame.size.height);
            }
        }
        index ++;
    }

    //楼栋
    y = self.roomNameLabel.frame.size.height + self.roomNameLabel.frame.origin.y + 4;
    width = [self calculateWidth:(self.frame.size.width - x - 32) string:self.roomInfoModel.bulidingName Font:15.0];
    self.buildingNameLabel.frame = CGRectMake(x, y, width, height);
    
    x += width + 10;
    width = [self calculateWidth:(self.frame.size.width - x - 32) string:self.roomInfoModel.floorName Font:15.0];
    self.floorNameLabel.frame = CGRectMake(x, y, width, height);
}


#pragma mark -----------lazy
-(UILabel *)roomNameLabel
{
    if (!_roomNameLabel) {
        _roomNameLabel = [[UILabel alloc]init];
        _roomNameLabel.font = [UIFont systemFontOfSize:16.0];
    }
    return _roomNameLabel;
}

-(UILabel*)floorNameLabel
{
    if (!_floorNameLabel) {
        _floorNameLabel = [[UILabel alloc]init];
        _floorNameLabel.font = [UIFont systemFontOfSize:15.0];
    }
    return _floorNameLabel;
}

-(UILabel *)buildingNameLabel
{
    if (!_buildingNameLabel) {
        _buildingNameLabel = [[UILabel alloc]init];
        _buildingNameLabel.font = [UIFont systemFontOfSize:15.0];
    }
    return _buildingNameLabel;
}

-(UIImageView *)roomImageView
{
    if (!_roomImageView) {
        _roomImageView = [[UIImageView alloc]init];
        _roomImageView.image = [UIImage imageNamed:@"assignTimeImage"];
    }
    return _roomImageView;
}

#pragma mark -- roomNo自适应长度
-(CGFloat)calculateWidth:(CGFloat)width string:(NSString *)str Font:(CGFloat)font
{
    CGSize size =CGSizeMake(width,20);
    
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:font],NSFontAttributeName,nil];
    
    //ios7方法，获取文本需要的size，限制宽度
    
    CGSize  actualsize =[str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    return actualsize.width;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
